package com.softeem.score.manager.controller.admin;

import com.softeem.score.manager.service.SysMenuService;
import com.softeem.score.manager.service.SysUserService;
import com.softeem.score.manager.service.ValidateCodeService;
import com.softeem.score.model.dto.LoginDto;
import com.softeem.score.model.entity.system.SysUser;
import com.softeem.score.model.vo.common.Result;
import com.softeem.score.model.vo.common.ResultCodeEnum;
import com.softeem.score.model.vo.system.LoginVo;
import com.softeem.score.model.vo.system.SysMenuVo;
import com.softeem.score.model.vo.system.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "用户接口")
@RestController
@RequestMapping(value = "/admin/system/index")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private ValidateCodeService validateCodeService;
    @Autowired
    private SysMenuService sysMenuService;

    @Operation(summary = "登录接口")
    @PostMapping(value = "/login")
    public Result login(@RequestBody LoginDto loginDto) {
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token") String token) {
        SysUser sysUser = sysUserService.getUserInfo(token);
        return Result.build(sysUser, ResultCodeEnum.SUCCESS);
    }

//    @GetMapping(value = "/getUserInfo")
//    public Result<SysUser> getUserInfo() {
//        return Result.build(AuthContextUtil.get(), ResultCodeEnum.SUCCESS);
//    }

    @Operation(summary = "生成验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {
        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();
        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "退出登录")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取用户菜单")
    @GetMapping("/menus")
    public Result menus() {
        List<SysMenuVo> sysMenuVoList = sysMenuService.findUserMenuList();
        return Result.build(sysMenuVoList, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "修改密码")
    @PutMapping("/updatePassword/{oldPassword}/{newPassword}")
    public Result updatePassword(@PathVariable("oldPassword") String oldPassword,
                                 @PathVariable("newPassword") String newPassword) {
        sysUserService.updatenewPassword(oldPassword,newPassword);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "重置密码")
    @PutMapping(value = "/restartPassword/{username}/{mobile}/{code}")
    public Result restartPassword(@PathVariable(value = "username") String username,
                                  @PathVariable(value = "mobile") String mobile,
                                  @PathVariable(value = "code") String code) {
        sysUserService.restartPassword(username, mobile, code);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

}